package com.bp.hearthealth.pulse.lnapps.ads.admob.interstitial_ad

import android.app.Activity
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback

import com.bp.hearthealth.pulse.lnapps.BpApplication
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsCount
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsSwitch
import com.bp.hearthealth.pulse.lnapps.ads.timer.TimerObserver
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError

object AdMobInterstitialAdManager : TimerObserver {

    private var adListener: AdMobInterstitialAdListener? = null
    private var mInterstitialAd: InterstitialAd? = null
    private var numberRetries = 0
    private var adUnitId = ""

    //是否资源
    fun release() {
        mInterstitialAd?.fullScreenContentCallback = null
        mInterstitialAd = null
        adListener = null
    }

    fun setInterstitialAdListener(listener: AdMobInterstitialAdListener?) {
        adListener = listener
    }

    // 加载插页式广告
    fun loadInterstitialAd(adUnitId: String) {
        if (!AdsSwitch.getBoolean(AdsSwitch.adInterControl)) {
            adListener?.onNotReady()
            return
        }
        AdMobInterstitialAdManager.adUnitId = adUnitId
        val adRequest = AdRequest.Builder().build()
        InterstitialAd.load(BpApplication.context,
            adUnitId,
            adRequest,
            object : InterstitialAdLoadCallback() {
                // 广告加载失败回调
                override fun onAdFailedToLoad(adError: LoadAdError) {
                    LogUtil.i("插页式广告加载失败: $adError")
                    if (numberRetries < 3) {
                        loadInterstitialAd(adUnitId)
                    }
                    numberRetries++
                    mInterstitialAd = null
                    adListener?.onAdFailedToLoad()
                }

                // 广告加载成功回调
                override fun onAdLoaded(interstitialAd: InterstitialAd) {
                    LogUtil.i("插页式广告加载成功")
                    mInterstitialAd = interstitialAd
                    adListener?.onAdLoaded(interstitialAd)
                    setupAdCallbacks()
                }
            })
    }

    // 设置广告回调
    private fun setupAdCallbacks() {
        mInterstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
            override fun onAdClicked() {
                LogUtil.i("插页式广告被点击")
                AdsCount.incrementClickCount()
                adListener?.onAdClicked()
            }

            override fun onAdDismissedFullScreenContent() {
                LogUtil.i("插页式广告完全关闭")
                mInterstitialAd = null
                adListener?.onAdDismissedFullScreenContent()
                loadInterstitialAd(adUnitId)
            }

            override fun onAdFailedToShowFullScreenContent(adError: AdError) {
                LogUtil.i("插页式广告展示失败: $adError")
                adListener?.onAdFailedToShowFullScreenContent()
                mInterstitialAd = null
            }

            override fun onAdImpression() {
                LogUtil.i("插页式广告印象被记录")
                adListener?.onAdImpression()
            }

            override fun onAdShowedFullScreenContent() {
                LogUtil.i("插页式广告完全展示")
                AdsCount.incrementDisplayCount()
                adListener?.onAdShowedFullScreenContent()
            }
        }
    }

    // 展示广告
    fun showInterstitialAd(activity: Activity) {
        if (!AdsSwitch.getBoolean(AdsSwitch.adInterControl)) {
            adListener?.onNotReady()
            return
        }
        if (numberRetries >= 2) {
            LogUtil.i("插页广告加载失败已超过3次")
            numberRetries = 0
            loadInterstitialAd(adUnitId)
        }
        if (mInterstitialAd != null) {
            mInterstitialAd?.show(activity)
        } else {
            adListener?.onNotReady()
            LogUtil.i("插页式广告尚未准备就绪")
        }
    }

    override fun clearCache() {
        LogUtil.i("观察者模式释放 插页广告资源")
        mInterstitialAd = null
        loadInterstitialAd(adUnitId)
    }
}